package com.gxpt.app.view;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.gxpt.app.AppUtils;
import com.gxpt.app.BaseActivity;
import com.gxpt.app.Constant;
import com.gxpt.app.MessageEvent;
import com.gxpt.app.R;
import com.gxpt.app.bean.addressdetail_bean;
import com.gxpt.app.bean.normal;
import com.utils.T;
import com.utils.logger.Logger;
import com.utils.okhttputils.HttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import okhttp3.Call;

/**
 * Created by dufeng on 2017/8/4.
 */

@ContentView(R.layout.activity_address_add_edit)
public class Activity_address_add_edit extends BaseActivity {

    @ViewInject(R.id.toback)
    Button toback;

    @ViewInject(R.id.txt_title)
    TextView txt_title;

    @ViewInject(R.id.txt_right)
    TextView txt_right;


    @ViewInject(R.id.put_1)
    EditText put_1;

    @ViewInject(R.id.put_2)
    EditText put_2;

    @ViewInject(R.id.put_3)
    EditText put_3;

    @ViewInject(R.id.put_8)
    TextView put_8;

    @ViewInject(R.id.img_default)
    CheckBox img_default;

    @ViewInject(R.id.toSelCity)
    RelativeLayout toSelCity;

    String province = null;
    String city     =null;
    String region   = null;

    int is_default=0;

    int ID = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        x.view().inject(this);
        initView();

        Bundle b = getIntent().getBundleExtra("DATA");
        if(b!=null){
            ID = Integer.parseInt(b.getString("ID"));
        }
        initData();
        Logger.i("ID = "+ID);
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(MessageEvent messageEvent){
        put_8.setText(messageEvent.getMessage());
        province = messageEvent.getProvince();
        city = messageEvent.getCity();
        region = messageEvent.getRegion();
    }

    @Override
    public void initView() {
        txt_title.setText(getResources().getString(R.string.txt_tianjiaxindizhi));
        txt_right.setVisibility(View.VISIBLE);

        img_default.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                // TODO Auto-generated method stub
                if(arg1){
                    is_default=1;
                }else{
                    is_default=0;
                }
            }
        });
    }


    @Override
    public void initData() {
        if(ID>0){
            toNet_addressdetail();
        }
    }

    private void setDATA(addressdetail_bean.addressdetail data){
        put_1.setText(data.recipient_name);
        put_2.setText(data.mobile_phone);
        put_3.setText(data.detail_address);
        put_8.setText(data.province+data.city+data.region);

        province = data.province_id;
        city     = data.city_id;
        region   = data.region_id;

        if(data.is_default.equals("1")){
            img_default.setChecked(true);
        }else{
            img_default.setChecked(false);
        }
    }


    @Event(value={R.id.toback,R.id.toSelCity,R.id.txt_right})
    private void getEvent(View view){
        switch(view.getId()){
            case R.id.toback:
                closeActivity();
                break;
            case R.id.toSelCity:
                jumpActivity(Activity_city_select_3.class,false);
                break;
            case R.id.txt_right:
                if(checkinput()){
                    toNet_storeaddress();
                }
                break;

        }
    }

    private boolean checkinput(){
        if (put_1.getText().toString().equals("")||put_2.getText().toString().equals("")||put_3.getText().toString().equals("")) {
            T.show(context, getResources().getString(R.string.alert_putisnotnull), 1);
            return false;
        } else {
            return true;
        }
    }


    /////NET
    private void toNet_storeaddress(){
        hud.show();
        JSONObject jsonBody=new JSONObject();
        try {
            jsonBody.put("device_number",         AppUtils.get_device_id(context));
            jsonBody.put("device_type",           Constant.device_type);
            jsonBody.put("token",                 AppUtils.get_token(context));
            jsonBody.put("pageName",             "shopaddress");
            jsonBody.put("serviceName",          "storeaddress");

            jsonBody.put("member_id",             AppUtils.get_user_id(context));
            jsonBody.put("id",                    ID);
            jsonBody.put("province",              province);//
            jsonBody.put("city",                  city);//
            jsonBody.put("region",                region);//
            jsonBody.put("recipient_name",        put_1.getText().toString());
            jsonBody.put("mobile_phone",          put_2.getText().toString());//
            jsonBody.put("detail_address",        put_3.getText().toString());//
            jsonBody.put("is_default",            is_default);//
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Logger.i(jsonBody.toString()+" ==["+Constant.URL_storeaddress+" ]");
        HttpUtils.http_post_string(Constant.URL_storeaddress,jsonBody.toString(),do_storeaddress);
    }

    //////NET DO
    StringCallback do_storeaddress = new StringCallback(){


        @Override
        public void onError(Call call, Exception e, int i) {
            Logger.e(e.getMessage());
        }

        @Override
        public void onResponse(String s, int i) {
            if(hud.isShowing()){
                hud.dismiss();
            }
            Logger.e(s);
            try{
                normal data = new Gson().fromJson(s, normal.class);

                if(data.state !=1 ){
                    T.show(context,data.msg,0);
                }else{
                    EventBus.getDefault().post(new MessageEvent(Constant.ACTION_TO_REFREASH));
                    finish();
                }
            }catch (Exception e){
                e.printStackTrace();
                T.show(context,getResources().getString(R.string.alert_again),0);
            }
        }
    };


    /////NET
    private void toNet_addressdetail(){
        hud.show();
        JSONObject jsonBody=new JSONObject();
        try {
            jsonBody.put("device_number",         AppUtils.get_device_id(context));
            jsonBody.put("device_type",           Constant.device_type);
            jsonBody.put("token",                 AppUtils.get_token(context));
            jsonBody.put("pageName",             "shopaddress");
            jsonBody.put("serviceName",          "addressdetail");

            jsonBody.put("member_id",             AppUtils.get_user_id(context));
            jsonBody.put("id",                    ID);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Logger.i(jsonBody.toString()+" ==["+Constant.URL_addressdetail+" ]");
        HttpUtils.http_post_string(Constant.URL_addressdetail,jsonBody.toString(),do_addressdetail);
    }

    //////NET DO
    StringCallback do_addressdetail = new StringCallback(){


        @Override
        public void onError(Call call, Exception e, int i) {
            Logger.e(e.getMessage());
        }

        @Override
        public void onResponse(String s, int i) {
            if(hud.isShowing()){
                hud.dismiss();
            }
            Logger.e(s);
            try{
                addressdetail_bean data = new Gson().fromJson(s, addressdetail_bean.class);

                if(data.state !=1 ){
                    T.show(context,data.msg,0);
                }else{
                    setDATA(data.data);
                }
            }catch (Exception e){
                e.printStackTrace();
                T.show(context,getResources().getString(R.string.alert_again),0);
            }
        }
    };



}
